package com.wdb007.venus.svc.biz.account;

import java.math.BigDecimal;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.wdb007.venus.svc.biz.utils.DateFormatUtil;
import com.wdb007.venus.svc.biz.utils.DateUtil;
import com.wdb007.venus.svc.dal.user.dao.UserCostFlagDAO;
import com.wdb007.venus.svc.dal.user.model.UserCostFlagDO;
import com.wdb007.venus.svc.facade.UserCostFlagService;
import com.wdb007.venus.svc.facade.account.model.UserCostFlagDTO;
import com.wdb007.venus.svc.facade.exception.BizException;

@Service("userCostFlagService")
public class UserCostFlagServiceImpl implements UserCostFlagService {

	private Logger logger = LoggerFactory.getLogger(UserCostFlagServiceImpl.class);
	
	@Autowired
	private UserCostFlagDAO userCostFlagDAO;
	
	@Override
	public int saveUserCostFlag(UserCostFlagDTO ucfDTO) throws BizException {
		logger.info("保存用户扣款信息: {}", JSON.toJSONString(ucfDTO));
		
		UserCostFlagDO query = new UserCostFlagDO();
		Long userId = ucfDTO.getUserId();
		Integer flag = ucfDTO.getFlag();		
		query.setUserId(userId);
		query.setFlag(flag);
		UserCostFlagDO result = userCostFlagDAO.getByCondition(query);
		if(3 == flag) {	//月卡扣费
			if(null == result) {
				//判断是否有年卡，如果有年卡，则直接在年卡的基础上加一个月。
				UserCostFlagDO query2 = new UserCostFlagDO();
				query2.setUserId(userId);
				query2.setFlag(4);
				UserCostFlagDO result2 = userCostFlagDAO.getByCondition(query2);
				Date startTime = new Date();
				if(null != result2) {
					startTime = DateFormatUtil.getMaxTimeDay(result2.getEndTime());
					
				}
				UserCostFlagDO userCostFlag = new UserCostFlagDO();
				userCostFlag.setUserId(userId);
				userCostFlag.setFlag(flag);
				userCostFlag.setCostOrder(3);
				userCostFlag.setCreateTime(new Date());
				userCostFlag.setValidFlag(1);
				userCostFlag.setStartTime(startTime);
				userCostFlag.setPrice(new BigDecimal("0.00"));
				if(null != ucfDTO.getDays()) {
					userCostFlag.setEndTime(DateFormatUtil.getMaxTimeDay(DateFormatUtil.getSomeDay(startTime, ucfDTO.getDays())));
				}else {
					userCostFlag.setEndTime(DateUtil.getNextMouth(startTime));
				}
				
				userCostFlag.setRemainder(ucfDTO.getRemainder());
				userCostFlagDAO.insert(userCostFlag);
			}else {
				if(null != ucfDTO.getDays()) {
					result.setEndTime(DateFormatUtil.getMaxTimeDay(DateFormatUtil.getSomeDay(result.getEndTime(), ucfDTO.getDays())));
				}else {
					result.setEndTime(DateFormatUtil.getMaxTimeDay(DateUtil.getNextMouth(result.getEndTime())));
				}
				result.setModifyTime(new Date());
				userCostFlagDAO.update(result);
			}
		}else if(4 == flag) { //年卡扣费
			//判断是否有月卡，如果有，则调整月卡的有效起始时间。
			UserCostFlagDO query2 = new UserCostFlagDO();
			query2.setUserId(userId);
			query2.setFlag(3);
			UserCostFlagDO result2 = userCostFlagDAO.getByCondition(query2);
			if(null != result2) {
				result2.setStartTime(DateFormatUtil.getMaxTimeDay(DateUtil.getNextYear(result2.getStartTime())));
				result2.setEndTime(DateFormatUtil.getMaxTimeDay(DateUtil.getNextYear(result2.getEndTime())));
				result2.setModifyTime(new Date());
				userCostFlagDAO.update(result2);
			}
			if(null == result) {
				UserCostFlagDO userCostFlag = new UserCostFlagDO();
				userCostFlag.setUserId(userId);
				userCostFlag.setCostOrder(4);
				userCostFlag.setCreateTime(new Date());
				userCostFlag.setFlag(flag);
				userCostFlag.setValidFlag(1);
				userCostFlag.setRemainder(ucfDTO.getRemainder());
				userCostFlag.setPrice(new BigDecimal("0.00"));
				userCostFlag.setStartTime(new Date());
				userCostFlag.setEndTime(DateFormatUtil.getMaxTimeDay(DateUtil.getNextYear(new Date())));
				userCostFlagDAO.insert(userCostFlag);
			}else {
				result.setEndTime(DateFormatUtil.getMaxTimeDay(DateUtil.getNextYear(result.getEndTime())));
				result.setModifyTime(new Date());
				userCostFlagDAO.update(result);
			}
		}else {
			if(null == result) {
				UserCostFlagDO userCostFlag = new UserCostFlagDO();
				userCostFlag.setUserId(userId);
				userCostFlag.setCostOrder(flag);
				userCostFlag.setCreateTime(new Date());
				userCostFlag.setFlag(flag);
				userCostFlag.setValidFlag(1);
				userCostFlag.setRemainder(ucfDTO.getRemainder());
				userCostFlag.setPrice(ucfDTO.getPrice());
				userCostFlag.setCreateTime(new Date());
				userCostFlagDAO.insert(userCostFlag);
			}else {
				result.setRemainder(result.getRemainder().add(ucfDTO.getRemainder()));
				result.setModifyTime(new Date());
				userCostFlagDAO.update(result);
			}
		}
		return 1;
	}

}
